
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta charset="utf-8" />
    <title>使用 DTrace 和 SystemTap 检测CPython &#8212; Python 3.7.8 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.8 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="Python 常见问题" href="../faq/index.html" />
    <link rel="prev" title="Argument Clinic How-To" href="clinic.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/howto/instrumentation.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="../faq/index.html" title="Python 常见问题"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="clinic.html" title="Argument Clinic How-To"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python 常用指引</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="instrumenting-cpython-with-dtrace-and-systemtap">
<span id="instrumentation"></span><h1>使用 DTrace 和 SystemTap 检测CPython<a class="headerlink" href="#instrumenting-cpython-with-dtrace-and-systemtap" title="永久链接至标题">¶</a></h1>
<dl class="field-list simple">
<dt class="field-odd">作者</dt>
<dd class="field-odd"><p>David Malcolm</p>
</dd>
<dt class="field-even">作者</dt>
<dd class="field-even"><p>Łukasz Langa</p>
</dd>
</dl>
<p>DTrace和SystemTap是监视工具，每个工具都提供了一种检查计算机系统上的进程正在执行的操作的方法。 它们都使用特定于域的语言，允许用户编写以下脚本：</p>
<blockquote>
<div><ul class="simple">
<li><p>进程监视的过滤器</p></li>
<li><p>从感兴趣的进程中收集数据</p></li>
<li><p>生成有关数据的报告</p></li>
</ul>
</div></blockquote>
<p>从Python 3.6开始，CPython可以使用嵌入式“标记”构建，也称为“探测器”，可以通过DTrace或SystemTap脚本观察，从而更容易监视系统上的CPython进程正在做什么。</p>
<div class="impl-detail compound">
<p><strong>CPython implementation detail:</strong> DTrace标记是CPython解释器的实现细节。 不保证CPython版本之间的探针兼容性。 更改CPython版本时，DTrace脚本可能会停止工作或无法正常工作而不会发出警告。</p>
</div>
<div class="section" id="enabling-the-static-markers">
<h2>启用静态标记<a class="headerlink" href="#enabling-the-static-markers" title="永久链接至标题">¶</a></h2>
<p>macOS内置了对DTrace的支持。 在Linux上，为了使用SystemTap的嵌入式标记构建CPython，必须安装SystemTap开发工具。</p>
<p>在Linux机器上，这可以通过：</p>
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> yum install systemtap-sdt-devel
</pre></div>
</div>
<p>或者：</p>
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> sudo apt-get install systemtap-sdt-dev
</pre></div>
</div>
<p>然后必须将CPython配置为``--with-dtrace``：</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>checking for --with-dtrace... yes
</pre></div>
</div>
<p>在macOS上，您可以通过在后台运行Python进程列出可用的DTrace探测器，并列出Python程序提供的所有探测器：</p>
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> python3.6 -q <span class="p">&amp;</span>
<span class="gp">$</span> sudo dtrace -l -P python<span class="nv">$!</span>  <span class="c1"># or: dtrace -l -m python3.6</span>

<span class="go">   ID   PROVIDER            MODULE                          FUNCTION NAME</span>
<span class="go">29564 python18035        python3.6          _PyEval_EvalFrameDefault function-entry</span>
<span class="go">29565 python18035        python3.6             dtrace_function_entry function-entry</span>
<span class="go">29566 python18035        python3.6          _PyEval_EvalFrameDefault function-return</span>
<span class="go">29567 python18035        python3.6            dtrace_function_return function-return</span>
<span class="go">29568 python18035        python3.6                           collect gc-done</span>
<span class="go">29569 python18035        python3.6                           collect gc-start</span>
<span class="go">29570 python18035        python3.6          _PyEval_EvalFrameDefault line</span>
<span class="go">29571 python18035        python3.6                 maybe_dtrace_line line</span>
</pre></div>
</div>
<p>在Linux上，您可以通过查看是否包含“.note.stapsdt”部分来验证构建的二进制文件中是否存在SystemTap静态标记。</p>
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> readelf -S ./python <span class="p">|</span> grep .note.stapsdt
<span class="go">[30] .note.stapsdt        NOTE         0000000000000000 00308d78</span>
</pre></div>
</div>
<p>如果您已将Python构建为共享库（使用--enable-shared），则需要在共享库中查找。 例如：</p>
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> readelf -S libpython3.3dm.so.1.0 <span class="p">|</span> grep .note.stapsdt
<span class="go">[29] .note.stapsdt        NOTE         0000000000000000 00365b68</span>
</pre></div>
</div>
<p>足够现代的readelf命令可以打印元数据：</p>
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> readelf -n ./python

<span class="go">Displaying notes found at file offset 0x00000254 with length 0x00000020:</span>
<span class="go">    Owner                 Data size          Description</span>
<span class="go">    GNU                  0x00000010          NT_GNU_ABI_TAG (ABI version tag)</span>
<span class="go">        OS: Linux, ABI: 2.6.32</span>

<span class="go">Displaying notes found at file offset 0x00000274 with length 0x00000024:</span>
<span class="go">    Owner                 Data size          Description</span>
<span class="go">    GNU                  0x00000014          NT_GNU_BUILD_ID (unique build ID bitstring)</span>
<span class="go">        Build ID: df924a2b08a7e89f6e11251d4602022977af2670</span>

<span class="go">Displaying notes found at file offset 0x002d6c30 with length 0x00000144:</span>
<span class="go">    Owner                 Data size          Description</span>
<span class="go">    stapsdt              0x00000031          NT_STAPSDT (SystemTap probe descriptors)</span>
<span class="go">        Provider: python</span>
<span class="go">        Name: gc__start</span>
<span class="go">        Location: 0x00000000004371c3, Base: 0x0000000000630ce2, Semaphore: 0x00000000008d6bf6</span>
<span class="go">        Arguments: -4@%ebx</span>
<span class="go">    stapsdt              0x00000030          NT_STAPSDT (SystemTap probe descriptors)</span>
<span class="go">        Provider: python</span>
<span class="go">        Name: gc__done</span>
<span class="go">        Location: 0x00000000004374e1, Base: 0x0000000000630ce2, Semaphore: 0x00000000008d6bf8</span>
<span class="go">        Arguments: -8@%rax</span>
<span class="go">    stapsdt              0x00000045          NT_STAPSDT (SystemTap probe descriptors)</span>
<span class="go">        Provider: python</span>
<span class="go">        Name: function__entry</span>
<span class="go">        Location: 0x000000000053db6c, Base: 0x0000000000630ce2, Semaphore: 0x00000000008d6be8</span>
<span class="go">        Arguments: 8@%rbp 8@%r12 -4@%eax</span>
<span class="go">    stapsdt              0x00000046          NT_STAPSDT (SystemTap probe descriptors)</span>
<span class="go">        Provider: python</span>
<span class="go">        Name: function__return</span>
<span class="go">        Location: 0x000000000053dba8, Base: 0x0000000000630ce2, Semaphore: 0x00000000008d6bea</span>
<span class="go">        Arguments: 8@%rbp 8@%r12 -4@%eax</span>
</pre></div>
</div>
<p>上述元数据包含SystemTap的信息，描述如何修补策略性放置的机器代码指令以启用SystemTap脚本使用的跟踪钩子。</p>
</div>
<div class="section" id="static-dtrace-probes">
<h2>静态DTrace探针<a class="headerlink" href="#static-dtrace-probes" title="永久链接至标题">¶</a></h2>
<p>下面的 DTrace 脚本示例可以用来显示一个 Python 脚本的调用/返回层次结构，只在调用名为 &quot;start&quot; 的函数内进行跟踪。换句话说，导入时的函数调用不会被列出。</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>self int indent;

python$target:::function-entry
/copyinstr(arg1) == &quot;start&quot;/
{
        self-&gt;trace = 1;
}

python$target:::function-entry
/self-&gt;trace/
{
        printf(&quot;%d\t%*s:&quot;, timestamp, 15, probename);
        printf(&quot;%*s&quot;, self-&gt;indent, &quot;&quot;);
        printf(&quot;%s:%s:%d\n&quot;, basename(copyinstr(arg0)), copyinstr(arg1), arg2);
        self-&gt;indent++;
}

python$target:::function-return
/self-&gt;trace/
{
        self-&gt;indent--;
        printf(&quot;%d\t%*s:&quot;, timestamp, 15, probename);
        printf(&quot;%*s&quot;, self-&gt;indent, &quot;&quot;);
        printf(&quot;%s:%s:%d\n&quot;, basename(copyinstr(arg0)), copyinstr(arg1), arg2);
}

python$target:::function-return
/copyinstr(arg1) == &quot;start&quot;/
{
        self-&gt;trace = 0;
}
</pre></div>
</div>
<p>它可以这样调用:</p>
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> sudo dtrace -q -s call_stack.d -c <span class="s2">&quot;python3.6 script.py&quot;</span>
</pre></div>
</div>
<p>输出结果会像这样:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>156641360502280  function-entry:call_stack.py:start:23
156641360518804  function-entry: call_stack.py:function_1:1
156641360532797  function-entry:  call_stack.py:function_3:9
156641360546807 function-return:  call_stack.py:function_3:10
156641360563367 function-return: call_stack.py:function_1:2
156641360578365  function-entry: call_stack.py:function_2:5
156641360591757  function-entry:  call_stack.py:function_1:1
156641360605556  function-entry:   call_stack.py:function_3:9
156641360617482 function-return:   call_stack.py:function_3:10
156641360629814 function-return:  call_stack.py:function_1:2
156641360642285 function-return: call_stack.py:function_2:6
156641360656770  function-entry: call_stack.py:function_3:9
156641360669707 function-return: call_stack.py:function_3:10
156641360687853  function-entry: call_stack.py:function_4:13
156641360700719 function-return: call_stack.py:function_4:14
156641360719640  function-entry: call_stack.py:function_5:18
156641360732567 function-return: call_stack.py:function_5:21
156641360747370 function-return:call_stack.py:start:28
</pre></div>
</div>
</div>
<div class="section" id="static-systemtap-markers">
<h2>静态SystemTap标记<a class="headerlink" href="#static-systemtap-markers" title="永久链接至标题">¶</a></h2>
<p>使用 SystemTap 集成的底层方法是直接使用静态标记。 这需要你显式地说明包含它们的二进制文件。</p>
<p>例如，这个SystemTap脚本可以用来显示Python脚本的调用/返回层次结构：</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>probe process(&quot;python&quot;).mark(&quot;function__entry&quot;) {
     filename = user_string($arg1);
     funcname = user_string($arg2);
     lineno = $arg3;

     printf(&quot;%s =&gt; %s in %s:%d\\n&quot;,
            thread_indent(1), funcname, filename, lineno);
}

probe process(&quot;python&quot;).mark(&quot;function__return&quot;) {
    filename = user_string($arg1);
    funcname = user_string($arg2);
    lineno = $arg3;

    printf(&quot;%s &lt;= %s in %s:%d\\n&quot;,
           thread_indent(-1), funcname, filename, lineno);
}
</pre></div>
</div>
<p>它可以这样调用:</p>
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> stap <span class="se">\</span>
  show-call-hierarchy.stp <span class="se">\</span>
  -c <span class="s2">&quot;./python test.py&quot;</span>
</pre></div>
</div>
<p>输出结果会像这样:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>11408 python(8274):        =&gt; __contains__ in Lib/_abcoll.py:362
11414 python(8274):         =&gt; __getitem__ in Lib/os.py:425
11418 python(8274):          =&gt; encode in Lib/os.py:490
11424 python(8274):          &lt;= encode in Lib/os.py:493
11428 python(8274):         &lt;= __getitem__ in Lib/os.py:426
11433 python(8274):        &lt;= __contains__ in Lib/_abcoll.py:366
</pre></div>
</div>
<p>其中的列是：</p>
<blockquote>
<div><ul class="simple">
<li><p>脚本开始后经过的微秒数</p></li>
<li><p>可执行文件的名字</p></li>
<li><p>进程的PID</p></li>
</ul>
</div></blockquote>
<p>其余部分则表示脚本执行时的调用/返回层次结构。</p>
<p>对于 <cite>--enable-shared</cite> 构建的CPython来说，这些标记是包含在libpython共享库中的，探针的点状路径需要反映这一点。比如上面例子中的这一行：</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>probe process(&quot;python&quot;).mark(&quot;function__entry&quot;) {
</pre></div>
</div>
<p>应改为：</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>probe process(&quot;python&quot;).library(&quot;libpython3.6dm.so.1.0&quot;).mark(&quot;function__entry&quot;) {
</pre></div>
</div>
<p>（假设是 CPython 3.6 的调试构建）</p>
</div>
<div class="section" id="available-static-markers">
<h2>可用的静态标记<a class="headerlink" href="#available-static-markers" title="永久链接至标题">¶</a></h2>
<dl class="function">
<dt id="c.function__entry">
<code class="sig-name descname">function__entry</code><span class="sig-paren">(</span>str<em> filename</em>, str<em> funcname</em>, int<em> lineno</em><span class="sig-paren">)</span><a class="headerlink" href="#c.function__entry" title="永久链接至目标">¶</a></dt>
<dd><p>这个标记表示一个Python函数的执行已经开始。它只对纯 Python （字节码）函数触发。</p>
<p>文件名、函数名和行号作为定位参数提供给跟踪脚本，必须使用 <code class="docutils literal notranslate"><span class="pre">$arg1</span></code>, <code class="docutils literal notranslate"><span class="pre">$arg2</span></code>, <code class="docutils literal notranslate"><span class="pre">$arg3</span></code> 访问：</p>
<blockquote>
<div><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">$arg1</span></code> : <code class="docutils literal notranslate"><span class="pre">(const</span> <span class="pre">char</span> <span class="pre">*)</span></code> 文件名，使用  <code class="docutils literal notranslate"><span class="pre">user_string($arg1)</span></code> 访问</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">$arg2</span></code> : <code class="docutils literal notranslate"><span class="pre">(const</span> <span class="pre">char</span> <span class="pre">*)</span></code> 函数名，使用 <code class="docutils literal notranslate"><span class="pre">user_string($arg2)</span></code> 访问</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">$arg3</span></code> : <code class="docutils literal notranslate"><span class="pre">int</span></code> 行号</p></li>
</ul>
</div></blockquote>
</dd></dl>

<dl class="function">
<dt id="c.function__return">
<code class="sig-name descname">function__return</code><span class="sig-paren">(</span>str<em> filename</em>, str<em> funcname</em>, int<em> lineno</em><span class="sig-paren">)</span><a class="headerlink" href="#c.function__return" title="永久链接至目标">¶</a></dt>
<dd><p>这个标记与 <a class="reference internal" href="#c.function__entry" title="function__entry"><code class="xref c c-func docutils literal notranslate"><span class="pre">function__entry()</span></code></a> 相反，表示Python函数的执行已经结束 (通过 <cite>return</cite> 或者异常)。 它只对纯Python (字节码) 函数触发。</p>
<p>参数和 <a class="reference internal" href="#c.function__entry" title="function__entry"><code class="xref c c-func docutils literal notranslate"><span class="pre">function__entry()</span></code></a> 相同</p>
</dd></dl>

<dl class="function">
<dt id="c.line">
<code class="sig-name descname">line</code><span class="sig-paren">(</span>str<em> filename</em>, str<em> funcname</em>, int<em> lineno</em><span class="sig-paren">)</span><a class="headerlink" href="#c.line" title="永久链接至目标">¶</a></dt>
<dd><p>这个标记表示一个 Python 行即将被执行。它相当于用 Python 分析器逐行追踪。它不会在C函数中触发。</p>
<p>参数和 <a class="reference internal" href="#c.function__entry" title="function__entry"><code class="xref c c-func docutils literal notranslate"><span class="pre">function__entry()</span></code></a> 相同</p>
</dd></dl>

<dl class="function">
<dt id="c.gc__start">
<code class="sig-name descname">gc__start</code><span class="sig-paren">(</span>int<em> generation</em><span class="sig-paren">)</span><a class="headerlink" href="#c.gc__start" title="永久链接至目标">¶</a></dt>
<dd><p>当Python解释器启动一个垃圾回收循环时被触发。 <code class="docutils literal notranslate"><span class="pre">arg0</span></code> 是要扫描的生成器，如 <a class="reference internal" href="../library/gc.html#gc.collect" title="gc.collect"><code class="xref py py-func docutils literal notranslate"><span class="pre">gc.collect()</span></code></a>。</p>
</dd></dl>

<dl class="function">
<dt id="c.gc__done">
<code class="sig-name descname">gc__done</code><span class="sig-paren">(</span>long<em> collected</em><span class="sig-paren">)</span><a class="headerlink" href="#c.gc__done" title="永久链接至目标">¶</a></dt>
<dd><p>当Python解释器完成一个垃圾收集循环时被触发。<code class="docutils literal notranslate"><span class="pre">arg0</span></code> 是收集到的对象的数量。</p>
</dd></dl>

<dl class="function">
<dt id="c.import__find__load__start">
<code class="sig-name descname">import__find__load__start</code><span class="sig-paren">(</span>str<em> modulename</em><span class="sig-paren">)</span><a class="headerlink" href="#c.import__find__load__start" title="永久链接至目标">¶</a></dt>
<dd><p>在 <a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a> 试图查找并加载模块之前被触发。<code class="docutils literal notranslate"><span class="pre">arg0</span></code> 是模块名称。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.7 新版功能.</span></p>
</div>
</dd></dl>

<dl class="function">
<dt id="c.import__find__load__done">
<code class="sig-name descname">import__find__load__done</code><span class="sig-paren">(</span>str<em> modulename</em>, int<em> found</em><span class="sig-paren">)</span><a class="headerlink" href="#c.import__find__load__done" title="永久链接至目标">¶</a></dt>
<dd><p>在 <a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a> 的 find_and_load 函数被调用后被触发 。<code class="docutils literal notranslate"><span class="pre">arg0</span></code> 是模块名称， <code class="docutils literal notranslate"><span class="pre">arg1</span></code> 表示模块是否成功加载。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.7 新版功能.</span></p>
</div>
</dd></dl>

</div>
<div class="section" id="systemtap-tapsets">
<h2>SystemTap Tapsets<a class="headerlink" href="#systemtap-tapsets" title="永久链接至标题">¶</a></h2>
<p>使用SystemTap集成的更高层次的方法是使用 &quot;tapset&quot; 。SystemTap 的等效库，它隐藏了静态标记的一些底层细节。</p>
<p>这里是一个基于 CPython 的非共享构建的 tapset 文件。</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>/*
   Provide a higher-level wrapping around the function__entry and
   function__return markers:
 \*/
probe python.function.entry = process(&quot;python&quot;).mark(&quot;function__entry&quot;)
{
    filename = user_string($arg1);
    funcname = user_string($arg2);
    lineno = $arg3;
    frameptr = $arg4
}
probe python.function.return = process(&quot;python&quot;).mark(&quot;function__return&quot;)
{
    filename = user_string($arg1);
    funcname = user_string($arg2);
    lineno = $arg3;
    frameptr = $arg4
}
</pre></div>
</div>
<p>如果这个文件安装在 SystemTap 的 tapset 目录下（例如``/usr/share/systemtap/tapset`` ），那么这些额外的探测点就会变得可用。</p>
<dl class="function">
<dt id="c.python.function.entry">
<code class="sig-name descname">python.function.entry</code><span class="sig-paren">(</span>str<em> filename</em>, str<em> funcname</em>, int<em> lineno</em>, frameptr<span class="sig-paren">)</span><a class="headerlink" href="#c.python.function.entry" title="永久链接至目标">¶</a></dt>
<dd><p>这个探针点表示一个Python函数的执行已经开始。它只对纯Python （字节码）函数触发。</p>
</dd></dl>

<dl class="function">
<dt id="c.python.function.return">
<code class="sig-name descname">python.function.return</code><span class="sig-paren">(</span>str<em> filename</em>, str<em> funcname</em>, int<em> lineno</em>, frameptr<span class="sig-paren">)</span><a class="headerlink" href="#c.python.function.return" title="永久链接至目标">¶</a></dt>
<dd><p>这个探针点是 <a class="reference internal" href="#c.python.function.return" title="python.function.return"><code class="xref c c-func docutils literal notranslate"><span class="pre">python.function.return()</span></code></a> 的反义操作，表示一个Python函数的执行已经结束（通过 <code class="docutils literal notranslate"><span class="pre">return</span></code> 或者异常）。 它只对纯Python （字节码）函数触发。</p>
</dd></dl>

</div>
<div class="section" id="examples">
<h2>例子<a class="headerlink" href="#examples" title="永久链接至标题">¶</a></h2>
<p>这个SystemTap脚本使用上面的tapset来更清晰地实现上面给出的跟踪Python函数调用层次结构的例子，而不需要直接命名静态标记。</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>probe python.function.entry
{
  printf(&quot;%s =&gt; %s in %s:%d\n&quot;,
         thread_indent(1), funcname, filename, lineno);
}

probe python.function.return
{
  printf(&quot;%s &lt;= %s in %s:%d\n&quot;,
         thread_indent(-1), funcname, filename, lineno);
}
</pre></div>
</div>
<p>下面的脚本使用上面的tapset提供了所有运行中的CPython代码的顶部视图，显示了整个系统中每一秒钟最频繁输入的前20个字节码帧。</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>global fn_calls;

probe python.function.entry
{
    fn_calls[pid(), filename, funcname, lineno] += 1;
}

probe timer.ms(1000) {
    printf(&quot;\033[2J\033[1;1H&quot;) /* clear screen \*/
    printf(&quot;%6s %80s %6s %30s %6s\n&quot;,
           &quot;PID&quot;, &quot;FILENAME&quot;, &quot;LINE&quot;, &quot;FUNCTION&quot;, &quot;CALLS&quot;)
    foreach ([pid, filename, funcname, lineno] in fn_calls- limit 20) {
        printf(&quot;%6d %80s %6d %30s %6d\n&quot;,
            pid, filename, lineno, funcname,
            fn_calls[pid, filename, funcname, lineno]);
    }
    delete fn_calls;
}
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">目录</a></h3>
  <ul>
<li><a class="reference internal" href="#">使用 DTrace 和 SystemTap 检测CPython</a><ul>
<li><a class="reference internal" href="#enabling-the-static-markers">启用静态标记</a></li>
<li><a class="reference internal" href="#static-dtrace-probes">静态DTrace探针</a></li>
<li><a class="reference internal" href="#static-systemtap-markers">静态SystemTap标记</a></li>
<li><a class="reference internal" href="#available-static-markers">可用的静态标记</a></li>
<li><a class="reference internal" href="#systemtap-tapsets">SystemTap Tapsets</a></li>
<li><a class="reference internal" href="#examples">例子</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="clinic.html"
                        title="上一章">Argument Clinic How-To</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="../faq/index.html"
                        title="下一章">Python 常见问题</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/howto/instrumentation.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="../faq/index.html" title="Python 常见问题"
             >下一页</a> |</li>
        <li class="right" >
          <a href="clinic.html" title="Argument Clinic How-To"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 常用指引</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2020, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 6月 29, 2020.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>2.3.1 创建。
    </div>

  </body>
</html>